<?php
/**
 * Object representing a user-content relation
 *
 * @package Concerto
 * @subpackage Models
 * @author Jason Raede <jason@torchdm.com>
 */
namespace application\models;

class UserContentRelation {

	/**
	 * ucr_id
	 *
	 * @var int
	 */
	protected $id;
	
	/**
	 * ucr_content
	 *
	 * @var int
	 */
	protected $content;
	
	/**
	 * ucr_user
	 *
	 * @var int
	 */
	protected $user;
	
	/**
	 * ucr_interaction
	 * 
	 * @var int
	 */
	protected $interaction;
	
	/**
	 * ucr_timestamp
	 *
	 * @var string
	 */
	protected $timestamp;
	
	/**
	 * ucr_content_qualifier
	 *
	 * @var string
	 */
	protected $contentQualifier;
	
	/**
	 * ucr_content_quantifier
	 *
	 * @var int
	 */
	protected $contentQuantifier;
	
	/**
	 * Initialize the object with its database information
	 *
	 * @param int $id
	 */
	public function __construct($id = null) {
		if($id) {
			$row = DB()->getRow("SELECT * FROM `cn_user_content_relations` WHERE `ucr_id`='$id'");
			$this->content = $row->ucr_content;
			$this->user = $row->ucr_user;
			$this->interaction = $row->ucr_interaction;
			$this->timestamp = $row->ucr_timestamp;
			$this->id = $id;
			$this->contentQualifier = $row->ucr_content_qualifier;
			$this->contentQuantifier = $row->ucr_content_quantifier;
		}
	}
	
	
	/**
	 * Update or insert the relation's database row
	 *
	 * @return array DBObject status array.
	 */
	public function save() {
		
		
		// Make sure the content exists
		if(!DB()->getVar("SELECT `content_id` FROM `cn_content` WHERE `content_id`='$this->content'")) {
			return array('status'=>'error', 'errors'=>array('Content (id: '.$this->content.') does not exist.'));
		}
		
		// Check if they made the same interaction with the same content in the past minute (to prevent spamming or mistakes)
		if(!$this->id) {
			$check = DB()->getVar("SELECT `ucr_id` FROM `cn_user_content_relations` WHERE `ucr_content`='$this->content' AND `ucr_user`='$this->user' AND UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(`ucr_timestamp`) < 18000");
			if($check) {
				return array('status'=>'error', 'errors'=>array('Too soon.'));
			}
		}
			
		
		// We're gonna create the interaction if it doesn't already exist - save time for users
		if(!is_numeric($this->interaction)) {
			$this->interaction = ucwords($this->interaction);
			$contentType = DB()->getVar("SELECT `content_type` FROM `cn_content` WHERE `content_id`='$this->content'");
			$interaction = DB()->getVar("SELECT `ci_id` FROM `cn_content_interactions` WHERE `ci_name` = '$this->interaction' && `ci_content_type`='$contentType'");
			if(!$interaction) {
				$interaction = new \application\models\Interaction;
				$interaction->create(array('contentType'=>$contentType, 'name'=>$this->interaction));
				$result = $interaction->save();
				$this->interaction = $result['id'];
			}
			else {
				$this->interaction = $interaction;
			}
		}
		
		
		$dbFields = array(
			'ucr_id'=>$this->id,
			'ucr_content'=>$this->content,
			'ucr_user'=>$this->user,
			'ucr_interaction'=>$this->interaction,
			'ucr_content_quantifier'=>$this->contentQuantifier,
			'ucr_content_qualifier'=>$this->contentQualifier
		);
		
		
		if($this->id) {
			DB()->update('cn_user_content_relations', 'ucr_id', $this->id, $dbFields);
			$id = $this->id;
			Modulator()->runFunction('modified_user_content_relation', $id);
		}
		else {
			DB()->insert('cn_user_content_relations', $dbFields);
			
			$id = DB()->lastInsert();
			Modulator()->runFunction('new_user_content_relation', $id);
		}
		return array('status'=>'success', 'id'=>$id));
	}
	
	/**
	 * Initialize the relation with the parameters provided
	 *
	 * @param array $params
	 */
	public function create($params) {
		global $concerto;
		if($params['content'] && $params['interaction']) {
			$this->content = $params['content'];
			if($params['user'])
				$this->user = $params['user'];
			elseif($concerto->User)
				$this->user = $concerto->User->getData('user_id');
			else
				return FALSE;
			$this->interaction = $params['interaction'];
			if($params['timestamp'])
				$this->timestamp = $params['timestamp'];
			if($params['quantifier'])
				$this->contentQuantifier = $params['quantifier'];
			if($params['qualifier'])
				$this->contentQualifier = $params['qualifier'];
		}
		else {
			return FALSE;
		}
	}
	
	/**
	 * Delete the relation from the database
	 *
	 * @return array DBObject status array
	 */
	public function delete() {
		DB()->query("DELETE FROM `cn_user_content_relations` WHERE `ucr_id`='".$this->id."'");
		return array('status'=>'success');
	}
}

?>